package com.mlz.mlzmall.ui.activity.act.personal;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.GetProductToExchangeResponserecord;
import com.mlz.mlzmall.ui.activity.act.address.postCodeToPCD;
import com.mlz.mlzmall.ui.activity.act.base.BaseActivity;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.LogUtils;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Pm on 2016/5/23.
 */
public class ForRecordActivity extends BaseActivity{
    private RelativeLayout iv_back_exchange_record;
    private ListView lv_recoder;
    private RelativeLayout rl_null_Intergation;
    public List<GetProductToExchangeResponserecord.ProductToExchangerecord.ListRecord> data;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forrecord);
        initView();
        getData();
    }

    private void getData() {
        tag = "ForRecordActivity_getData";
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.EXCHANGE.HISTORYLIST, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
             //   showToast(jsonObject.toString());
                LogUtils.d("积分历史记录"+jsonObject.toString());
                Gson gson = new Gson();
                GetProductToExchangeResponserecord bean = gson.fromJson(jsonObject.toString(),GetProductToExchangeResponserecord.class);
                data = bean.getData().getList();
                if(data!=null&&data.size()>0)
                {
              //      showToast(data.size()+"");
                    lv_recoder.setAdapter(new MyAdapter());
                    lv_recoder.setVisibility(View.VISIBLE);
                }else{
                    rl_null_Intergation.setVisibility(View.VISIBLE);
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(tag, volleyError.getMessage(), volleyError);
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };
        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

    private void initView() {
        iv_back_exchange_record = (RelativeLayout)findViewById(R.id.iv_back_exchange_record);
        lv_recoder = (ListView)findViewById(R.id.lv_recoder);
        rl_null_Intergation = (RelativeLayout)findViewById(R.id.rl_null_Intergation);
        iv_back_exchange_record.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.iv_back_exchange_record:
                finish();
                break;
            default:
                break;
        }
    }
    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_forrecord, null);
            ViewHolder vh = new ViewHolder();
            vh.record_picture = (ImageView) convertView.findViewById(R.id.record_picture);
            vh.tv_address_particular = (TextView) convertView.findViewById(R.id.tv_address_particular);
            vh.tv_time_record = (TextView) convertView.findViewById(R.id.tv_time_record);
            vh.tv_price_transportation= (TextView) convertView.findViewById(R.id.tv_price_transportation);
            vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            vh.tv_telephone= (TextView) convertView.findViewById(R.id.tv_telephone);
            if (convertView != null) {
                StringBuffer sba = new StringBuffer();
                int regionCode = data.get(position).getShippingRegionCode();
//                sba.append(postCodeToPCD.province(String.valueOf(regionCode), IntegrationExchangeRecord.this));
//                sba.append(postCodeToPCD.city(String.valueOf(regionCode), IntegrationExchangeRecord.this));
//                sba.append(postCodeToPCD.district(String.valueOf(regionCode), IntegrationExchangeRecord.this));
                sba.append(data.get(position).getShippingAddress());
                //获取省市区
                postCodeToPCD ppp=new postCodeToPCD(ForRecordActivity.this);//获取省市县
                String code=String.valueOf(data.get(position).getShippingRegionCode());
                String province=ppp.province(code+"");
                String city=ppp.city(code+"");
                String area=ppp.district(code+"");
                vh.tv_address_particular.setText(province+city+area+sba.toString());

                // .setText(data.get(position).getShippingAddress());
                vh.tv_price_transportation.setText(String.valueOf(data.get(position).getFreight()));

                vh.tv_name.setText(data.get(position).getShippingFullname());
                vh.tv_telephone.setText(data.get(position).getShippingTelephone());
                imageLoader.displayImage(data.get(position).getImage(), vh.record_picture, options);

                //将后台的long型开始时间转换为正常日期并显示
                Date dat_start = new Date(data.get(position).getDateAdded());
                java.util.GregorianCalendar gc = new java.util.GregorianCalendar();
                gc.setTime(dat_start);
                SimpleDateFormat formatstart =new SimpleDateFormat("yyyy-MM-dd");
                String sb = formatstart.format(gc.getTime());
                vh.tv_time_record.setText(sb);

            }

            return convertView;
        }

        class ViewHolder {
            ImageView record_picture;
            TextView tv_address_particular;
            TextView tv_time_record;
            TextView tv_price_transportation;
            TextView tv_name;
            TextView tv_telephone;
        }
    }
}
